home *** CD-ROM | disk | FTP | other *** search
- /*****
- *
- * What's On My Mac
- *
- * By Scott T Boyd (with help from Grant Neufeld & Jorg... )
- *
- * GrabFrontWindow.c
- *
- * Copyright ©1995 by Scott T Boyd
- *
- * This source may be freely used as long as the copyright notice is kept in the source.
- * I (we) ask that you let us know of any enhancements (read: bug fixes) to this code.
- * I (we) would also like copies of (or discounts on) anything you produce this with, please.
- *
- * http://www.hax.com/
- * scott@hax.com
- *
- *****/
-
- #include <string.h>
- #include "Layers.h"
- #include "ImageCompression.h"
- #include "Sound.h"
- #include "GrabFrontWindow.h"
-
-
- //Play the camera click
- void
- SnapshotSound ( void )
- {
- short err;
- SndChannelPtr chan;
- SndListHandle sndHdl;
- Boolean async;
-
- chan = NULL;
- sndHdl = (SndListHandle) GetResource ( 'snd ', 0xbf88 );
- async = false;
-
- err = SndPlay ( chan, sndHdl, async );
-
- ReleaseResource ( (Handle)sndHdl );
- }
-
-
- void
- GrabFrontWindowAsJPEG ( unsigned short *width, unsigned short *height )
- {
- LayerPtr rootLayer;
- WindowPtr frontmostWindow;
- Rect frontmostRect;
- short theRefNum;
- short vRefNum;
- Str255 fileName;
- long startTime;
- OSErr err;
- Ptr compressedResultPtr;
- PixMapHandle frontmostPixMap;
- ImageDescriptionHandle imageDescriptionHandle;
-
- startTime = TickCoun t();
-
- /* 4000000 is a totally arbitrary 'very big' value */
- compressedResultPtr = NewPtr ( 4000000 );
- err = MemError ();
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- imageDescriptionHandle = (ImageDescriptionHandle) NewHandle ( sizeof(ImageDescription) );
- err = MemError ();
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- // Find the front layer
- rootLayer = GetRootLayer ();
-
- // get the first non-floating window
- frontmostWindow = GetSelectable ( rootLayer );
-
- //save the port
- // GetPort(&thePort);
-
- // SetPort(frontmostWindow);
- frontmostRect = frontmostWindow->portRect;
-
- //remember the dimensions so we can tell the caller
- *width = frontmostRect.right - frontmostRect.left;
- *height = frontmostRect.bottom - frontmostRect.top;
-
- /* "What's the point of doing a hack here without all the help?" */
-
- /* "Do you want me to show you how that works?"
- -- Eric Traut, to Sean Parent */
-
- // GetCWMgrPort(&wMgrCPort);
- // SetPort((GrafPtr)wMgrCPort);
-
- //Grab the bits out of the frontmost window and compress them with the jpeg compressor
- frontmostPixMap = ((CGrafPtr)frontmostWindow)->portPixMap;
-
- err = CompressImage ( frontmostPixMap, &frontmostRect, codecNormalQuality, 'jpeg', imageDescriptionHandle, compressedResultPtr );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- // SetPort(thePort);
-
- /* "they're running out of ideas, for sure"
- -- Grant Neufeld, on Hollywood, for opening logo sequences */
-
- //Create a file and stuff it full of the jpeg data
-
- GetVol ( fileName, &vRefNum );
- strcpy ( (char*)fileName, (char*)"\pfrontwindow.jpg" );
-
- err = FSDelete ( fileName, vRefNum );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- err = Create ( fileName, vRefNum, 'JVWR', 'JPEG' );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- err = FSOpen ( fileName, vRefNum, &theRefNum );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- {
- long count;
-
- count = (*imageDescriptionHandle)->dataSize;
- err = FSWrite ( theRefNum, &count, compressedResultPtr );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
- }
- err = FSClose ( theRefNum );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- //clean up
- DisposeHandle ( (Handle)imageDescriptionHandle );
- DisposePtr ( compressedResultPtr );
- }
-
-
- /* this function doesn't really work :-(
- It's supposed to move the mouse to the coordinates h & v (local) in the frontmostwindow's
- port (converting to global coordinates) and post a click event at that location */
- void
- ClickOnTheFrontWindow ( unsigned short h, unsigned short v )
- {
- long eventMsg;
- short err;
- Point thePt;
- LayerPtr rootLayer;
- WindowPtr frontmostWindow;
- GrafPtr thePort;
- EvQElPtr eventPtr;
-
- // Find the front layer
- rootLayer = GetRootLayer();
- // get the first non-floating window
- frontmostWindow = GetSelectable(rootLayer);
-
- //save the port
- GetPort ( &thePort );
- SetPort ( frontmostWindow );
-
- thePt.h = h;
- thePt.v = v;
-
- LocalToGlobal ( &thePt );
-
- eventMsg = (((long)thePt.v)<<16) + thePt.h;
-
- err = PPostEvent ( mouseDown, eventMsg, &eventPtr );
-
- eventPtr->evtQWhere = thePt;
-
- //restore the port
- SetPort ( thePort );
- }
-